package com.everlasting.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.everlasting.shiro.JwtUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;

/**
 * 自动填充字段
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Subject currentUser = SecurityUtils.getSubject();
        Long tenantId = JwtUtil.getUsername(currentUser.getPrincipals().toString());
        this.setInsertFieldValByName("create_by", "Jerry", metaObject);//@since 快照：3.0.7.2-SNAPSHOT， @since 正式版暂未发布3.0.7
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("operator", "Tom", metaObject);
        //this.setUpdateFieldValByName("operator", "Tom", metaObject);//@since 快照：3.0.7.2-SNAPSHOT， @since 正式版暂未发布3.0.7
    }
}

